package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catelog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
public class indexController {

    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate redisTemplate;


    @GetMapping({"/", "index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> level1Categorys = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", level1Categorys);
        return "index";
    }

    @ResponseBody
    @RequestMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCatlogJson() {
        Map<String, List<Catelog2Vo>> map = categoryService.getCatelogJson();
        return map;
    }

    @ResponseBody
    @RequestMapping("/hello")
    public String hello() {
        //1.获取一把锁，只要锁的名字一样，就是同一把锁
        RLock lock = redisson.getLock("my-lock");
        //2.加锁
        lock.lock(30, TimeUnit.SECONDS);//30秒自动解锁,自动解锁时间一定要大于业务的执行时间---不会自动续期，引发删一个没有的key
        //1.锁的自动续期，如果业务超长，运行期间自动给锁续期新的30s，不用担心业务时间长，锁自动过期被删掉
        //2.加锁的业务只要运行完成，就不会给当前锁续期，即使不手动解锁，锁默认在30s以后自动删除
        //只要占锁成功，就会启动一个定时任务【重新给锁设置过期时间，新的过期时间就是看门狗的默认时间】
        // 【看门狗时间】/3,10s

        //最佳方案
        // lock.lock(30, TimeUnit.SECONDS);省掉整个续期操作。手动解锁
        try {
            System.out.println("加锁成功，执行业务---" + Thread.currentThread().getId());
            Thread.sleep(10000);
        } catch (Exception e) {
        } finally {
            //3.解锁
            System.out.println("释放锁---" + Thread.currentThread().getId());
            lock.unlock();
        }
        return "hello";
    }


    @GetMapping("/write")
    @ResponseBody
    public String write() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        //1.改数据加写锁，读数据加读锁
        RLock rLock = lock.writeLock();
        try {
            rLock.lock();
            s = UUID.randomUUID().toString();
            Thread.sleep(30000);
            redisTemplate.opsForValue().set("writeValue", s);
        } catch (Exception e) {
        } finally {
            rLock.unlock();
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String read() {
        RReadWriteLock lock = redisson.getReadWriteLock("rw-lock");
        String s = "";
        RLock rLock = lock.readLock();
        rLock.lock();
        try {
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {

        }finally {
            rLock.unlock();
        }
        return s;
    }
}
